home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DESDOS.ARJ / GETPASS.C < prev    next >
Text File  |  1992-05-15  |  1KB  |  77 lines

  1. #include <stdio.h>
  2. #include <signal.h>
  3.  
  4. #include <bios.h>
  5.  
  6. /* Issue prompt and read reply with echo turned off */
  7. char *
  8. getpass(prompt)
  9. char *prompt;
  10. {
  11.  
  12. #ifndef MSDOS
  13.     struct sgttyb ttyb,ttysav;
  14.     FILE *tty;
  15. #endif
  16.  
  17.     register char *cp;
  18.     int c;
  19.     static char pbuf[128];
  20.  
  21. #ifndef MSDOS
  22.     int (*signal())(),(*sig)();
  23. #else
  24.     int (*sig)();
  25. #endif
  26.  
  27. #ifndef MSDOS
  28.     if ((tty = fdopen(open(TTY, 2), "r")) == NULL)
  29.         tty = stdin;
  30.     else
  31.         setbuf(tty, (char *)NULL);
  32. #endif
  33.  
  34.     sig = signal(SIGINT, SIG_IGN);
  35.  
  36. #ifndef MSDOS
  37.     ioctl(fileno(tty), TIOCGETP, &ttyb);
  38.     ioctl(fileno(tty), TIOCGETP, &ttysav);
  39.     ttyb.sg_flags |= RAW;
  40.     ttyb.sg_flags &= ~ECHO;
  41.     ioctl(fileno(tty), TIOCSETP, &ttyb);
  42. #endif
  43.  
  44.     fprintf(stderr, "%s", prompt);
  45.     fflush(stderr);
  46.     cp = pbuf;
  47.     for (;;) {
  48.  
  49. #ifndef MSDOS
  50.         c = getc(tty);
  51. #else
  52.         c = (int) bioskey(0);
  53. #endif
  54.  
  55.         if(c == '\r' || c == '\n' || c == EOF)
  56.             break;
  57.         if (cp < &pbuf[127])
  58.             *cp++ = c;
  59.     }
  60.     *cp = '\0';
  61.     fprintf(stderr,"\r\n");
  62.     fflush(stderr);
  63.  
  64. #ifndef MSDOS
  65.     ioctl(fileno(tty), TIOCSETP, &ttysav);
  66. #endif
  67.  
  68.     signal(SIGINT, SIG_IGN);
  69.  
  70. #ifndef MSDOS
  71.     if (tty != stdin)
  72.         fclose(tty);
  73. #endif
  74.  
  75.     return(pbuf);
  76. }
  77.